<template>
  <div class="login">
    <div class="box">
      <div class="title">火星</div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item prop="userName">
          <el-input
            prefix-icon="el-icon-user"
            v-model="ruleForm.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="userPwd">
          <el-input
            prefix-icon="el-icon-lock"
            show-password
            type="password"
            v-model="ruleForm.userPwd"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
            <el-button style="width:100%;" type="primary" @click="doLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        userName: "",
        userPwd: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
        userPwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {},
  methods: {
      doLogin(){
          this.$refs['ruleForm'].validate(async valid=>{
              if(valid){
                  let res = this.userApi.login(this.ruleForm)
                  this.$store.commit('saveUserInfo', res)
                  this.$message.success('登录成功')
                  this.$router.push('/layout');
              }else{
                  return false;
              }
          })
      }
  },
};
</script>

<style scoped lang="scss">
.login {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f9fcff;
  .box {
    width: 500px;
    padding: 50px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 0 10px 5px #c7c9cb4d;
    .title {
      font-size: 45px;
      line-height: 1.5;
      text-align: center;
      margin-bottom: 25px;
    }
  }
}
</style>
